<?php
// ### AclAssistant : Item

$this->useClass("Action_Community_FeedHandler");
class Action_Community_PersonShow extends Action_Community_FeedHandler
{
	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderItems");

		return new AggregatedModel_Feed_ReaderItems();
	}

	var $ajax_functions = array(
		'ajax_getSomePosts',
		);

	public function ajax_getSomePosts( $page_number ) {

		$this->setParam("page_number", $page_number);

		$this->model->doStuff();

		$view = $this->rh->views->findByName('items:member');
		$view->data = $this->acl_assistant->template_data;
		$items = $this->model->getItems();
		foreach ($items as $i=>$item) {
			$items[ $i ]['type'] = $this->tdi_types[ $item['post']['tdi_oid'] ];
		}
		$view->data['items'] = $items;

		$items_node = &$this->rh->ctx->cp->add_node("items");
		$view->data['tag_sections']['single'] = count($this->getParam('tdis')) == 1 || isset($this->params['tdi_url']);

		$items_node->set_data( $view->getContent() ); // рендеринг итемов

		$this->addPagerNodes( &$this->rh->ctx->cp );
	}

	protected function determineCustomModelParams() {
		// --- author --- {{{
		if (isset($this->params['oid'])) {
			$person_oid = $this->params['oid'];
			$o = $this->rh->ctx->store->getObject($person_oid);
			$this->setParam('author_oid', $o->id);
		}
		// --- author --- }}}
	}

	protected function init() {

		parent::init();

		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids);

		$this->aa = $this->module->aa;
		$this->ctx->mm->setAclAssistant($this->aa);
		$this->rh->UseClass("RL_URLGenerator");

		$this->community = $this->ctx->store->getObject($this->aa->acl_data['comity_oid']);
		$this->m_person = $this->ctx->mm->getModel('Person', 'm');
		$this->m_item_author = $this->ctx->mm->getModel('ItemAuthor', 'm');
	}

	public function getWidgetsConfig ()
	{
		$widgets = array();

		$widgets[] = array (
			'name' => 'TagsWidget',
			'params' => array(
				'community_oid' => $this->aa->acl_data['comity_oid'],
				'tdi_oid'=> $this->aa->acl_data['tdi_oid'],
				'tag' => $this->getParam('tag'),
				'widget_position' => 'tags',
				'widget_view' => 'WG_TagsWidget',
				'user_oid'=>$this->aa->acl_data['user_id'],
			),
		);

		$widgets[] = array (
			'name' => 'Feed_TdiMenuWidget',
			'params' => array(
				'community_oid' => $this->aa->acl_data['comity_oid'],
				'tdi_oid'=> $this->aa->acl_data['tdi_oid'],
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_TdiMenuWidget',
			),
		);

		$widgets[] = array (
			'name' => 'RssWidget',
			'params' => array(
				'widget_view' => 'WG_RssWidget',
				'widget_position' => 'tags',
			),
		);

		return $widgets;
	} // end of function getWidgetsConfig

	private function processItems($items) {
		foreach ($items as $i=>$item) {
			if ($item['post']['type'] == 'fvote') {
				// установить bool $item['voted'] если текущий пользователь голосовал
				$vm = $this->ctx->mm->getModel("Vote", "m");
				$aa = $this->aa;
				$items[$i]['already_voted'] = $vm->checkUserVote($item['post']['oid'], $aa->acl_data['user_id']);

				// создать ссылку на голосоватор за данный итем
				$ug = new RL_URLGenerator($this->rh->router->getRule("vote_manager")); //
				$ug->setRouter($this->rh->router);
				$ug->setParams(array(
					"community_url"=>$this->params['community_url'],
					"id_item"=>$item['post']['oid'],
					)
				);
				$items[$i]['vote_url'] = $ug->generateUrl();
				unset($ug);

				if ($item['post']['posted_by_id'] == $this->aa->acl_data['user_id']) {
					if ( $item['vote_closed'] ) {
						$items[$i]['can_open'] = 1;
					} else {
						$items[$i]['can_close'] = 1;
					}
				}

				$items[$i]['can_vote'] = (isset($this->aa->acl_data['user_id'])) ? true : false;
			}
			$items[$i]['type'] = $this->tdi_types[ $item['post']['tdi_oid'] ];
			$items[$i]['acl'] = $this->aa->getAcl( $item['post']['oid'] );
		}
		return $items;
	}

	public function process ()
	{
		$aa = $this->aa;

		$aa->checkDeletions( array($this, "shutdown") );

		$this->model->doStuff();

		$view = $this->rh->views->findByName('person:show');
		$view->data = $aa->template_data;

		$view->data['can_create'] = $aa->is_granted(array('item_create', 'item_create_premod'));

		$items = $this->model->getItems();

		if ($items) {
			$view->data['items'] = $this->processItems($items);
		} else $view->data['message'] = "В этой ленте нет сообщений.";

		// данные для постраничной листалки
		$pager = $this->getPagerInfo();
		$view->data['page'] = $pager;
		$view->data['items_remaining'] = $pager['items_remaining'];

		// -- sections -- {{{
		if ( isset($this->params['tdi_url']) ) {
			$view->data['defaultsection'] = $this->params['tdi_url'];
			$view->data['tdi_url'] = $this->params['tdi_url'];
			$view->data['tdi'] = $this->tdi_types[ $this->aa->acl_data['tdi_oid'] ];
			$view->data['tdi_title'] = $this->tdi_types[ $this->aa->acl_data['tdi_oid'] ]['title'];
			$view->data['tdi_color'] = $this->tdi_types[ $this->aa->acl_data['tdi_oid'] ]['color'];
		}

		$tag = $this->getParam('tag'); // instanceof fobject

		if ($tag) {
			$view->data['current_tag'] = $tag->data;
			$view->data['tag'] = $tag;
		}
		$view->data['tag_sections']['single'] = count($tdis) == 1 || isset($this->params['tdi_url']);
		// --- sections --- }}}

		$view->data['__widgets__'] = $this->widgets;

		// генерация урл адресов
		$this->rh->UseClass("RL_URLGenerator");
		$ug = new RL_URLGenerator($this->rh->router->getLastMatched()); //$this->rh->router->getRule('object_undelete')
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		// урл для постраничной листалки без ajax
		if (isset($pager['prev'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['prev'])));
			$view->data["prev_url"] = $ug->generateUrl();
		}

		if (isset($pager['next'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['next'])));
			$view->data["next_url"] = $ug->generateUrl();
		}

		unset($ug);

		// для постраничной листалки ajax handler
		$ug = new RL_URLGenerator($this->rh->router->getRule('showmember_ajax'));

		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);
		$ug->setGet($this->rh->GET);

		$view->data['ajax_url'] = $ug->generateUrl();

		// собственно персона {{{
		$person_oid = $this->params['oid'];
		$person = $this->m_person->getPerson($person_oid);

		$aa->getAvailableOperations($person->id);

		$view->data['person'] = $person->data;
		$view->data['person']['avatar'] = $this->ctx->store->getObject($person->data['avatar'])->data;
		$view->data['person']['user'] = $this->m_person->getUser($person)->data;
		if ($aa->is_granted(array('item_edit'))) {
			$view->data['person']['do_edit']['url'] = $this->hrefEditPerson($person);
			$view->data['person']['do_edit']['title'] = 'Редактировать';
		}
		// собственно персона }}}

		// ShowPerson specific variables

		$this->rh->response->setContent( $view );

		// -- touch visitor stat -- {{{
		// visit member
		$this->ctx->mm->getModel("Visitable", "m")->getVisitable('Person')->setWhat($person->id)->visit();
		//
		// -- touch visitor stat -- }}}

	} // end of function process

	public function hrefFilterByTag ($tag)
	{
		$rule = 'showmember';

		$ug = new RL_URLGenerator($this->rh->router->getRule($rule));
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);
		$GET = $this->rh->GET;
		unset($GET[$this->rq_tag]);
		if ($tag === 'all') {
			$ug->setGet($GET);
		} else {
			$ug->setGet(array_merge($GET, array($this->rq_tag=>$tag)));
		}
		return $ug->generateUrl();
	} // end of method hrefFilterByDoctype

	public function hrefEditPerson($person)
	{
		$rule = 'person_edit';

		$ug = new RL_URLGenerator($this->rh->router->getRule($rule));
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);
		$GET = $this->rh->GET;
		$url = $ug->generateUrl();
		return $url;
	} // end of method hrefEditPerson
}
